﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarPolicyInfoDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarPolicyInfoDB
    {
        public CarPolicyInfoDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarPolicyInfo carpolicyinfo)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarPolicyInfoInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarPolicyInfoId", carpolicyinfo.ProviderCarPolicyId);
            _sPara.Add("@ProviderCompanyId", carpolicyinfo.ProviderCompanyId);
            _sPara.Add("@PolicyInfoTitle", carpolicyinfo.PolicyInfoTitle);
            _sPara.Add("@PersonalAccidentLimit", carpolicyinfo.PersonalAccidentLimit);
            _sPara.Add("@WindscreenAccess", carpolicyinfo.WindscreenAccess);
            _sPara.Add("@TowingCharges", carpolicyinfo.TowingCharges);
            _sPara.Add("@ExcessNamedDriver", carpolicyinfo.ExcessNamedDriver);
            _sPara.Add("@ExcessUnnamedDriver", carpolicyinfo.ExcessUnnamedDriver);
            _sPara.Add("@QuoteExpiryDate", carpolicyinfo.QuoteExpiryDate);
            _sPara.Add("@CancellationPenalty", carpolicyinfo.CancellationPenalty);
            _sPara.Add("@RefCEUserId", carpolicyinfo.RefCEUserId);
            _sPara.Add("@Status", carpolicyinfo.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 ProviderCarPolicyId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarPolicyInfoDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarPolicyInfoId", ProviderCarPolicyId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;

        }

        public CarPolicyInfoList GetProviderCarPolicyInfoList(Int64 ProviderCompanyId)
        {
            CarPolicyInfoList _CarPolicyInfolist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarPolicyInfoSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _CarPolicyInfolist = new CarPolicyInfoList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarPolicyInfo _CarPolicyInfo = new CarPolicyInfo();
                        _CarPolicyInfo = FillDataRecord(_dt, i);
                        _CarPolicyInfolist.Add(_CarPolicyInfo);
                    }
                }
            }
            return _CarPolicyInfolist;
        }

        public CarPolicyInfo GetProviderCarPolicyInfo(Int64 ProviderCarPolicyId)
        {
            CarPolicyInfo _carpolicyinfo = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarPolicyInfoSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarPolicyInfoId", ProviderCarPolicyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carpolicyinfo = new CarPolicyInfo();
                    _carpolicyinfo = FillDataRecord(_dt, 0);
                }
            }
            return _carpolicyinfo;
        }


        private CarPolicyInfo FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarPolicyInfo myCarPolicyInfo = new CarPolicyInfo();
            myCarPolicyInfo.ProviderCarPolicyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarPolicyInfoId"].ToString());
            myCarPolicyInfo.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarPolicyInfo.PolicyInfoTitle = myDataTable.Rows[RowId]["PolicyInfoTitle"].ToString();
            myCarPolicyInfo.PersonalAccidentLimit = float.Parse(myDataTable.Rows[RowId]["PersonalAccidentLimit"].ToString());
            myCarPolicyInfo.WindscreenAccess = float.Parse(myDataTable.Rows[RowId]["WindscreenAccess"].ToString());
            myCarPolicyInfo.TowingCharges = float.Parse(myDataTable.Rows[RowId]["TowingCharges"].ToString());
            myCarPolicyInfo.ExcessNamedDriver = float.Parse(myDataTable.Rows[RowId]["ExcessNamedDriver"].ToString());
            myCarPolicyInfo.ExcessUnnamedDriver = float.Parse(myDataTable.Rows[RowId]["ExcessUnnamedDriver"].ToString());
            myCarPolicyInfo.QuoteExpiryDate = DateTime.Parse(myDataTable.Rows[RowId]["QuoteExpiryDate"].ToString());
            myCarPolicyInfo.CancellationPenalty = float.Parse(myDataTable.Rows[RowId]["CancellationPenalty"].ToString());
            myCarPolicyInfo.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarPolicyInfo.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarPolicyInfo.ModifiedDate = DateTime.Parse(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarPolicyInfo;

        }

    }
}
